{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[W 210310 10:35:53 __init__:203] atx-agent has something wrong, auto recovering\n",
      "[D 210310 10:35:53 __init__:287] device D3H7N18706002467 is online\n",
      "[I 210310 10:35:53 init:155] uiautomator2 version: 2.13.0\n",
      "[I 210310 10:35:53 init:352] Install minicap, minitouch\n",
      "[I 210310 10:35:54 init:367] Install com.github.uiautomator, com.github.uiautomator.test 2.3.1\n",
      "[I 210310 10:35:56 init:332] - app-uiautomator.apk installed\n",
      "[I 210310 10:35:57 init:332] - app-uiautomator-test.apk installed\n",
      "[I 210310 10:35:57 init:377] Check atx-agent version\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Successfully init AdbDevice(serial=D3H7N18706002467)\n",
      "打开支付宝\n",
      "打开蚂蚁森林，等待5s……\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[D 210310 10:36:00 __init__:600] kill process(ps): uiautomator\n",
      "[D 210310 10:36:01 __init__:618] uiautomator-v2 is starting ... left: 40.0s\n",
      "[D 210310 10:36:02 __init__:618] uiautomator-v2 is starting ... left: 39.0s\n",
      "[D 210310 10:36:03 __init__:618] uiautomator-v2 is starting ... left: 37.9s\n",
      "[D 210310 10:36:04 __init__:618] uiautomator-v2 is starting ... left: 36.9s\n",
      "[D 210310 10:36:05 __init__:618] uiautomator-v2 is starting ... left: 35.9s\n",
      "[D 210310 10:36:06 __init__:618] uiautomator-v2 is starting ... left: 34.9s\n",
      "[D 210310 10:36:07 __init__:618] uiautomator-v2 is starting ... left: 33.8s\n",
      "[D 210310 10:36:09 __init__:618] uiautomator-v2 is starting ... left: 32.8s\n",
      "[D 210310 10:36:10 __init__:618] uiautomator-v2 is starting ... left: 31.8s\n",
      "[I 210310 10:36:10 __init__:583] uiautomator back to normal\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开始第1次偷能量！\n",
      "开始第2次偷能量！\n",
      "开始第3次偷能量！\n",
      "开始第4次偷能量！\n",
      "开始第5次偷能量！\n",
      "开始第6次偷能量！\n",
      "开始第7次偷能量！\n",
      "开始第8次偷能量！\n",
      "开始第9次偷能量！\n",
      "开始第10次偷能量！\n",
      "开始第11次偷能量！\n",
      "开始第12次偷能量！\n",
      "开始第13次偷能量！\n",
      "开始第14次偷能量！\n",
      "开始第15次偷能量！\n",
      "开始第16次偷能量！\n",
      "开始第17次偷能量！\n",
      "开始第18次偷能量！\n",
      "开始第19次偷能量！\n",
      "开始第20次偷能量！\n",
      "开始第21次偷能量！\n",
      "开始第22次偷能量！\n",
      "开始第23次偷能量！\n",
      "开始第24次偷能量！\n",
      "###结束###\n"
     ]
    }
   ],
   "source": [
    "import uiautomator2 as u2\n",
    "import time\n",
    "import random\n",
    "d = u2.connect()  # 有线连接，手机需要插电脑上 \n",
    "#d = u2.connect(\"192.168.0.108\") #通过无线连接，电脑和手机需要在同一个局域网内，并且需要先用有线的方式做过初始化\n",
    "\n",
    "# d.app_stop(\"com.eg.android.AlipayGphone\") \n",
    "\n",
    "print(\"打开支付宝\")\n",
    "d.app_start(\"com.eg.android.AlipayGphone\")\n",
    "time.sleep(2) ## 休眠2s等待支付宝完全启动\n",
    "\n",
    "\n",
    "print(\"打开蚂蚁森林，等待5s……\")\n",
    "d(text=\"蚂蚁森林\").click()\n",
    "time.sleep(5) ## 我手机比较卡，进入蚂蚁森林后还需要几秒钟才能完全加载完   \n",
    "\n",
    "def collectEnergy(cnt):\n",
    "    print(\"开始第%d次偷能量！\" % cnt)\n",
    "    # 开始扫描点击有能力出现的区域  \n",
    "    for x in range(150,1000,150):\n",
    "        for y in range(600,900,150):\n",
    "            d.long_click(x + random.randint(10,20), y + random.randint(10,20), 0.1)\n",
    "            time.sleep(0.01)\n",
    "            if cnt != 1:\n",
    "                d.click(536,1816)\n",
    "\n",
    "cnt = 1\n",
    "while True:\n",
    "    collectEnergy(cnt)\n",
    "    a = d.xpath(\"//*[@resource-id='J_tree_dialog_wrap']\").get().bounds \n",
    "    d.click(1000, a[3]-80) # 找能量按钮的坐标 \n",
    "    ## 如果页面出现了“返回我的森林”说明已经没有能量可偷了，结束\n",
    "    if d.xpath('//*[@text=\"返回我的森林\"]').click_exists(timeout=2.0):\n",
    "        break\n",
    "    cnt += 1\n",
    "print(\"###结束###\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
